package com.bentley.kotlindemo.ui.kits.authorlist_paging

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.bentley.kotlindemo.R
import com.bentley.kotlindemo.db.entity.ArticleItemBean
import com.facebook.drawee.view.SimpleDraweeView

/**
 * <pre>
 *  ┌────────────────────────────────────────────
 *  │ @author  Lee  @date 2021/3/25 11:23
 *  ├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄
 *  │ @desc  分页适配器
 *  └────────────────────────────────────────────
 * </pre>
 */

class AuthorPagingAdapter :
    PagingDataAdapter<ArticleItemBean, AuthorViewHolder>(DataDifferntiator) {

    class ViewHolder(view: View) : RecyclerView.ViewHolder(view)

    override fun onBindViewHolder(holder: AuthorViewHolder, position: Int) {
//        holder.itemView.textViewName.text =
//            "${getItem(position)?.firstName} ${getItem(position)?.lastName}"
//        holder.itemView.textViewEmail.text = getItem(position)?.email
        holder.textViewName?.text =
            "${getItem(position)?.id} ${getItem(position)?.id}"
        holder.textViewEmail?.text = getItem(position)?.first_name

        holder.ivPortrait?.setImageURI(getItem(position)?.avatar)
    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AuthorViewHolder {
        return AuthorViewHolder(
            LayoutInflater
                .from(parent.context)
                .inflate(R.layout.item_authorlist_paging, parent, false)
        )
    }

    object DataDifferntiator : DiffUtil.ItemCallback<ArticleItemBean>() {

        override fun areItemsTheSame(oldItem: ArticleItemBean, newItem: ArticleItemBean): Boolean {
            return oldItem.id == newItem.id
        }

        override fun areContentsTheSame(oldItem: ArticleItemBean, newItem: ArticleItemBean): Boolean {
            return oldItem == newItem
        }
    }


}

class AuthorViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

    public var textViewName: TextView? = null
    public var textViewEmail: TextView? = null
    public var ivPortrait: SimpleDraweeView? = null

    init {
        textViewName = itemView.findViewById(R.id.textViewName)
        textViewEmail = itemView.findViewById(R.id.textViewEmail)
    }

//    fun bind(user: User) {
//        tvName?.text = user.name
//        tvAge?.text = user.age.toString()
//    }
}
